package net.tv90.volunteerserveradmin.utils;

public class CsvUtils {

    public static String escapeCsvField(String field) {
        if (field == null) {
            return "";
        }
        // 如果字段包含逗号、换行或者双引号，则需要加双引号，并对内部双引号进行转义
        boolean needQuotes = field.contains(",") || field.contains("\"") || field.contains("\n") || field.contains("\r");
        if (needQuotes) {
            // 将内部的 " 替换为 ""
            field = field.replace("\"", "\"\"");
            // 外部再包上一层引号
            return "\"" + field + "\"";
        } else {
            return field;
        }
    }
}
